home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / hlrad / qradutil.cpp < prev    next >
C/C++ Source or Header  |  2002-12-09  |  6KB  |  220 lines

  1. #include "qrad.h"
  2.  
  3. static dplane_t backplanes[MAX_MAP_PLANES];
  4.  
  5. dleaf_t*        PointInLeaf(const vec3_t point)
  6. {
  7.     int             nodenum;
  8.     vec_t           dist;
  9.     dnode_t*        node;
  10.     dplane_t*       plane;
  11.  
  12.     nodenum = 0;
  13.     while (nodenum >= 0)
  14.     {
  15.         node = &g_dnodes[nodenum];
  16.         plane = &g_dplanes[node->planenum];
  17.         dist = DotProduct(point, plane->normal) - plane->dist;
  18.         if (dist >= 0.0)
  19.         {
  20.             nodenum = node->children[0];
  21.         }
  22.         else
  23.         {
  24.             nodenum = node->children[1];
  25.         }
  26.     }
  27.  
  28.     return &g_dleafs[-nodenum - 1];
  29. }
  30.  
  31. /*
  32.  * ==============
  33.  * PatchPlaneDist
  34.  * Fixes up patch planes for brush models with an origin brush
  35.  * ==============
  36.  */
  37. vec_t           PatchPlaneDist(const patch_t* const patch)
  38. {
  39.     const dplane_t* plane = getPlaneFromFaceNumber(patch->faceNumber);
  40.  
  41.     return plane->dist + DotProduct(g_face_offset[patch->faceNumber], plane->normal);
  42. }
  43.  
  44. void            MakeBackplanes()
  45. {
  46.     int             i;
  47.  
  48.     for (i = 0; i < g_numplanes; i++)
  49.     {
  50.         backplanes[i].dist = -g_dplanes[i].dist;
  51.         VectorSubtract(vec3_origin, g_dplanes[i].normal, backplanes[i].normal);
  52.     }
  53. }
  54.  
  55. const dplane_t* getPlaneFromFace(const dface_t* const face)
  56. {
  57.     if (!face)
  58.     {
  59.         Error("getPlaneFromFace() face was NULL\n");
  60.     }
  61.  
  62.     if (face->side)
  63.     {
  64.         return &backplanes[face->planenum];
  65.     }
  66.     else
  67.     {
  68.         return &g_dplanes[face->planenum];
  69.     }
  70. }
  71.  
  72. const dplane_t* getPlaneFromFaceNumber(const unsigned int faceNumber)
  73. {
  74.     dface_t*        face = &g_dfaces[faceNumber];
  75.  
  76.     if (face->side)
  77.     {
  78.         return &backplanes[face->planenum];
  79.     }
  80.     else
  81.     {
  82.         return &g_dplanes[face->planenum];
  83.     }
  84. }
  85.  
  86. // Returns plane adjusted for face offset (for origin brushes, primarily used in the opaque code)
  87. void getAdjustedPlaneFromFaceNumber(unsigned int faceNumber, dplane_t* plane)
  88. {
  89.     dface_t*        face = &g_dfaces[faceNumber];
  90.     const vec_t*    face_offset = g_face_offset[faceNumber];
  91.  
  92.     plane->type = (planetypes)0;
  93.     
  94.     if (face->side)
  95.     {
  96.         vec_t dist;
  97.  
  98.         VectorCopy(backplanes[face->planenum].normal, plane->normal);
  99.         dist = DotProduct(plane->normal, face_offset);
  100.         plane->dist = backplanes[face->planenum].dist + dist;
  101.     }
  102.     else
  103.     {
  104.         vec_t dist;
  105.  
  106.         VectorCopy(g_dplanes[face->planenum].normal, plane->normal);
  107.         dist = DotProduct(plane->normal, face_offset);
  108.         plane->dist = g_dplanes[face->planenum].dist + dist;
  109.     }
  110. }
  111.  
  112. // Will modify the plane with the new dist
  113. void            TranslatePlane(dplane_t* plane, const vec_t* delta)
  114. {
  115.     vec3_t          proj;
  116.     vec_t           magnitude;
  117.  
  118.     ProjectionPoint(delta, plane->normal, proj);
  119.     magnitude = VectorLength(proj);
  120.  
  121.     if (DotProduct(plane->normal, delta) > 0)              //if zero, magnitude will be zero.
  122.     {
  123.         plane->dist += magnitude;
  124.     }
  125.     else
  126.     {
  127.         plane->dist -= magnitude;
  128.     }
  129. }
  130.  
  131. // HuntForWorld will never return CONTENTS_SKY or CONTENTS_SOLID leafs
  132. dleaf_t*        HuntForWorld(vec_t* point, const vec_t* plane_offset, const dplane_t* plane, int hunt_size, vec_t hunt_scale, vec_t hunt_offset)
  133. {
  134.     dleaf_t*        leaf;
  135.     int             x, y, z;
  136.     int             a;
  137.  
  138.     vec3_t          current_point;
  139.     vec3_t          original_point;
  140.  
  141.     vec3_t          best_point;
  142.     dleaf_t*        best_leaf = NULL;
  143.     vec_t           best_dist = 99999999.0;
  144.  
  145.     vec3_t          scales;
  146.  
  147.     dplane_t        new_plane = *plane;
  148.  
  149.     if (hunt_scale < 0.1)
  150.     {
  151.         hunt_scale = 0.1;
  152.     }
  153.  
  154.     scales[0] = 0.0;
  155.     scales[1] = -hunt_scale;
  156.     scales[2] = hunt_scale;
  157.  
  158.     VectorCopy(point, best_point);
  159.     VectorCopy(point, original_point);
  160.  
  161.     TranslatePlane(&new_plane, plane_offset);
  162.  
  163.     if (!hunt_size)
  164.     {
  165.         hunt_size = DEFAULT_HUNT_SIZE;
  166.     }
  167.  
  168.     for (a = 1; a < hunt_size; a++)
  169.     {
  170.         for (x = 0; x < 3; x++)
  171.         {
  172.             current_point[0] = original_point[0] + (scales[x % 3] * a);
  173.             for (y = 0; y < 3; y++)
  174.             {
  175.                 current_point[1] = original_point[1] + (scales[y % 3] * a);
  176.                 for (z = 0; z < 3; z++)
  177.                 {
  178.                     vec3_t          delta;
  179.                     vec_t           dist;
  180.  
  181.                     current_point[2] = original_point[2] + (scales[z % 3] * a);
  182.  
  183.                     SnapToPlane(&new_plane, current_point, hunt_offset);
  184.                     VectorSubtract(current_point, original_point, delta);
  185.                     dist = VectorLength(delta);
  186.  
  187.                     if (dist < best_dist)
  188.                     {
  189.                         if ((leaf = PointInLeaf(current_point)) != g_dleafs)
  190.                         {
  191.                             if ((leaf->contents != CONTENTS_SKY) && (leaf->contents != CONTENTS_SOLID))
  192.                             {
  193.                                 if (x || y || z)
  194.                                 {
  195.                                     //dist = best_dist;
  196.                                     best_leaf = leaf;
  197.                                     VectorCopy(current_point, best_point);
  198.                                     continue;
  199.                                 }
  200.                                 else
  201.                                 {
  202.                                     VectorCopy(current_point, point);
  203.                                     return leaf;
  204.                                 }
  205.                             }
  206.                         }
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.         if (best_leaf)
  212.         {
  213.             break;
  214.         }
  215.     }
  216.  
  217.     VectorCopy(best_point, point);
  218.     return best_leaf;
  219. }
  220.